package com.jinzhi.jzapi.utils;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by guoy on 2017/2/10.
 * jar版本：poi-3.9-20121203.jar, poi-ooxml-3.9-20121203.jar,
 *         poi-ooxml-schemas-3.9-20121203.jar, itextpdf-5.5.9.jar, itext-asian-5.2.0.jar
 * 初步实现IText转pdf的功能
 * 注：由于excel中style样式和itext中样式映射上有区别，
 * 比如excel读出来字体是20，设置itext对象为20，然而生成出来的字体却偏大到22的样子
 * */
public class ITextPdfUtil {

    /**
     * 设置相关参数
     * @param document
     * @return
     */
    public static Document setParameters(Document document, String title, String subject, String keywords, String author,
                                         String creator){
        // 设置标题
        document.addTitle(title);
        // 设置主题
        document.addSubject(subject);
        // 设置作者
        document.addKeywords(keywords);
        // 设置作者
        document.addAuthor(author);
        // 设置创建者
        document.addCreator(creator);
        // 设置生产者
        document.addProducer();
        // 设置创建日期
        document.addCreationDate();

        return document;
    }

/**
     * @param pdfFilePath        pdf文件地址
     * @param watermark          标识内容
     * @param fontSize           字体大小
     * @param fontRgb            rgb颜色串
     * @param position           设置起始位置 宽度、高度
     * @param angleOfinclination 倾斜角度，如30，45
 * */
    public static void doAddWaterMark(String pdfFilePath, String watermark, float fontSize, String fontRgb,
                                       String position, int angleOfinclination) {
        for (String filepath : pdfFilePath.split(",")) {
            try {
                addWaterMark(filepath, watermark, fontSize, fontRgb, position, angleOfinclination);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (DocumentException e) {
                e.printStackTrace();
            }
        }
    }

    private static void addWaterMark(String pdfFilePath, String watermark, float fontSize, String fontRgb,
                                     String position, int angleOfinclination)
            throws IOException, DocumentException {
        String tempfilepath = pdfFilePath + ".tmp";
        File tempFile = new File(tempfilepath);
        PdfReader reader = new PdfReader(pdfFilePath);// 待加水印的文件
        PdfStamper stamper = new PdfStamper(reader,
                new FileOutputStream(tempFile));
        int total = reader.getNumberOfPages() + 1;
        PdfContentByte content;
        BaseFont font = BASE_FONT_CHINESE;//默认字体
        //int textWidth = 200;
        //int textHeight = 50;
        String[] colArr = fontRgb.replaceAll("[ \r\n]", "").split(",");
        String[] posArr = position.replaceAll("[ \r\n]", "").split(",");
        int textWidth = Integer.parseInt(posArr[0]);
        int textHeight = Integer.parseInt(posArr[1]);
        BaseColor color = new BaseColor(Integer.parseInt(colArr[0]), Integer.parseInt(colArr[1]),
                Integer.parseInt(colArr[2]));
        for (int i = 1; i < total; i++)//循环对每页插入水印
        {
            content = stamper.getUnderContent(i);//水印的起始
            content.beginText();//开始
            content.setColorFill(color);//设置颜色
            content.setFontAndSize(font, fontSize);//设置字体及字号38
            content.setTextMatrix(textWidth, textHeight);//设置起始位置
            content.showTextAligned(Element.ALIGN_LEFT, watermark, textWidth, textHeight, angleOfinclination);//开始写入水印
            content.endText();
        }
        stamper.close();
        reader.close();
        File destFile = new File(pdfFilePath);
        destFile.delete();
        tempFile.renameTo(destFile);
    }

    protected static BaseFont BASE_FONT_CHINESE;

    static {
        try {
            BASE_FONT_CHINESE = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
